package com.wbao.dianniu.adapter;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.Context;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.wbao.dianniu.R;

public class NoScrollGridAdapter extends BaseAdapter{
	
	private Context mContext;
	private ArrayList<String> mList;
	private HashMap<Integer, View> viewMap; 
	
	public NoScrollGridAdapter(Context context,ArrayList<String> datalist) {
		mContext = context;
		mList = datalist;
		viewMap = new HashMap<>();
	}

	public void setData(ArrayList<String> dataList){
		mList = dataList;
	}
	
	@Override
	public int getCount() {
		if (null == mList)
			return 0;
		else
			return mList.size();
	}

	@Override
	public Object getItem(int position) {
		if(position < mList.size()){
			return mList.get(position);
		}else{
			return 0;
		}
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		 final Holder holder;
		if(!viewMap.containsKey(position) || viewMap.get(position) == null){  
			holder = new Holder();  
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_gridview, null);  
            holder.imageview = (ImageView) convertView.findViewById(R.id.iv_image);  
            convertView.setTag(holder);  
            viewMap.put(position, convertView);  
        }else{  
//        	System.out.println("   viewMap  is not  null ");
            convertView = viewMap.get(position);
            holder = (Holder) convertView.getTag(); 
        }

		Glide.with(mContext).load(mList.get(position)).centerCrop().crossFade()
				.thumbnail(0.1f).into(holder.imageview);
        return convertView;
	}

	private class Holder{
		ImageView imageview;
	}
	
}
